﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using BLL;
using System.IO;


namespace IAATO
{
    public partial class SiteMaster : MasterPage
    {
        private const string AntiXsrfTokenKey = "__AntiXsrfToken";
        private const string AntiXsrfUserNameKey = "__AntiXsrfUserName";
        private string _antiXsrfTokenValue;

        private NavireService navireService;
        private CompagnieService compagnieService;

        protected void Page_Init(object sender, EventArgs e)
        {
            // The code below helps to protect against XSRF attacks
            var requestCookie = Request.Cookies[AntiXsrfTokenKey];
            Guid requestCookieGuidValue;
            if (requestCookie != null && Guid.TryParse(requestCookie.Value, out requestCookieGuidValue))
            {
                // Use the Anti-XSRF token from the cookie
                _antiXsrfTokenValue = requestCookie.Value;
                Page.ViewStateUserKey = _antiXsrfTokenValue;
            }
            else
            {    
                // Generate a new Anti-XSRF token and save to the cookie
                _antiXsrfTokenValue = Guid.NewGuid().ToString("N");
                Page.ViewStateUserKey = _antiXsrfTokenValue;

                var responseCookie = new HttpCookie(AntiXsrfTokenKey)
                {
                    HttpOnly = true,
                    Value = _antiXsrfTokenValue
                };
                if (FormsAuthentication.RequireSSL && Request.IsSecureConnection)
                {
                    responseCookie.Secure = true;
                }
                Response.Cookies.Set(responseCookie);
            }

            Page.PreLoad += master_Page_PreLoad;
        }

        protected void master_Page_PreLoad(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Set Anti-XSRF token
                ViewState[AntiXsrfTokenKey] = Page.ViewStateUserKey;
                ViewState[AntiXsrfUserNameKey] = Context.User.Identity.Name ?? String.Empty;
            }
            else
            {
                // Validate the Anti-XSRF token
                if ((string)ViewState[AntiXsrfTokenKey] != _antiXsrfTokenValue
                    || (string)ViewState[AntiXsrfUserNameKey] != (Context.User.Identity.Name ?? String.Empty))
                {
                    throw new InvalidOperationException("Validation of Anti-XSRF token failed.");
                }
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Roles.RoleExists("Admin"))
            {
                Roles.CreateRole("Admin");
            }

            if (!Roles.RoleExists("Navire"))
            {
                Roles.CreateRole("Navire");
            }

            if (!Roles.RoleExists("Secrétaire"))
            {
                Roles.CreateRole("Secrétaire");
            }



            if (!IsPostBack)
            {
                navireService = new NavireService(new NavireManager());
                compagnieService = new CompagnieService(new CompagnieManager());

                List<COMPAGNIE> compagnies = compagnieService.getAllCompagnies();

                foreach (COMPAGNIE c in compagnies) {
                    MenuItem mtiCompagnie = new MenuItem();
                    mtiCompagnie.Text = c.NOM;

                    List<NAVIRE> navires = navireService.getNaviresByCompagnieId(c.ID);

                    if (navires != null) {
                        menNavigationMenu.Items[1].ChildItems.Add(mtiCompagnie);

                        foreach (NAVIRE n in navires)
                        {
                            MenuItem mtiNavire = new MenuItem();
                            mtiNavire.Text = n.NOM;
                            mtiNavire.NavigateUrl = "~/ProgrammeManagement.aspx?id=" + n.ID;

                            mtiCompagnie.ChildItems.Add(mtiNavire);
                        }
                    }
                }

                setMenu();
            }
        }

        protected void setMenu()
        {
            if (HttpContext.Current.User.IsInRole("Admin"))
            {
                /*liUtilisateurs.Visible = true;
                liCompagnies.Visible = true;
                liNavires.Visible = true;
                liProgrammes.Visible = true;
                liActivites.Visible = true;
                liSites.Visible = true;
                liZones.Visible = true;
                liLinks.Visible = true;*/
            }
            else if (HttpContext.Current.User.IsInRole("Secrétaire"))
            {
                menNavigationMenu.Items[2].ChildItems.RemoveAt(0);
                menNavigationMenu.Items[2].ChildItems.RemoveAt(0);
                menNavigationMenu.Items[2].ChildItems.RemoveAt(1);
                menNavigationMenu.Items[2].ChildItems.RemoveAt(2);
                menNavigationMenu.Items[2].ChildItems.RemoveAt(2);


                /*liUtilisateurs.Visible = false;
                liCompagnies.Visible = false;
                liNavires.Visible = true;
                liProgrammes.Visible = true;
                liActivites.Visible = false;
                liSites.Visible = true;
                liZones.Visible = false;
                liLinks.Visible = false;*/
            }
            else if (HttpContext.Current.User.IsInRole("Navire"))
            {
                menNavigationMenu.Items.RemoveAt(2);

                MenuItem programme = menNavigationMenu.Items[1];
                programme.Text = "Other programs";

                MenuItem monProgramme = new MenuItem();
                monProgramme.Text = "My program";
                string navireUserName = HttpContext.Current.User.Identity.Name;
                string[] splitUserName = navireUserName.Split('_');
                monProgramme.NavigateUrl = "~/ProgrammeManagement.aspx?id=" + splitUserName[splitUserName.Length - 1];

                menNavigationMenu.Items.AddAt(1, monProgramme);

                /*liUtilisateurs.Visible = false;
                liCompagnies.Visible = false;
                liNavires.Visible = false;
                liProgrammes.Visible = true;
                liActivites.Visible = false;
                liSites.Visible = false;
                liZones.Visible = false;
                liLinks.Visible = false;*/
            }
            else
            {
                menNavigationMenu.Items.RemoveAt(1);
                menNavigationMenu.Items.RemoveAt(1);

                /*liUtilisateurs.Visible = false;
                liCompagnies.Visible = false;
                liNavires.Visible = false;
                liProgrammes.Visible = false;
                liActivites.Visible = false;
                liSites.Visible = false;
                liZones.Visible = false;
                liLinks.Visible = false;*/
            }
        }
    }
}